package com.ll.domain.dingtalk.base.impl;

import com.ll.dal.po.CoursePO;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.ll.dal.po.RankingPO;
import com.ll.domain.dingtalk.base.DingTalkMessageBaseService;
import com.ll.domain.ranking.dto.RankingDTO;
import com.ll.domain.ranking.enums.RankingTypeEnum;
import com.taobao.api.ApiException;
import com.ll.base.util.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class DingTalkMessageBaseServiceImpl implements DingTalkMessageBaseService {

    private static String[] tokens = {
            //管理班子测试机器人
//            "https://oapi.dingtalk.com/robot/send?access_token=0077a0ed44c0aebe18d6523aed1c7974e00c40344be022c4a4b31f62982309a0"

            //大群机器人
            "https://oapi.dingtalk.com/robot/send?access_token=6ab67a78cfd032a3dd7ce11be0080500375efd3e32b8345d20d7cd5fec269d7c",
    };


    @Override
    public void sendCourseNotify(CoursePO courseDTO) {
//        for (String robotToken : tokens) {
//            DingTalkClient client = new DefaultDingTalkClient(robotToken);
//            OapiRobotSendRequest request = new OapiRobotSendRequest();
//            request.setMsgtype("markdown");
//            OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
//            markdown.setTitle("开课通知");
//            markdown.setText("#### 开课通知:" + courseDTO.getTeacher() + "发起 " + courseDTO.getType().name() + "\n" +
//                    "> 训练时间为：" + DateUtil.parseDateToStr(courseDTO.getStartTime(), DateUtil.DATE_FORMAT_MMDDHHMI) +
//                    "~" + DateUtil.parseDateToStr(courseDTO.getEndTime(), "HH:mm") + "\n\n" +
//                    "> 报名方式：\n\n" +
//                    ">  1.通过手机钉钉小程序\"弓箭派\"报名”\n\n" +
//                    ">  2.通过艾特我查看课程表 快速报名”\n\n" +
//                    "> ![screenshot](http://47.111.20.91:8080/invite.jpg)\n");
//            request.setMarkdown(markdown);
//            try {
//                OapiRobotSendResponse response = client.execute(request);
//            } catch (ApiException e) {
//                e.printStackTrace();
//            }
//        }
        this.sendMsg("开课通知", "#### 开课通知:" + courseDTO.getTeacher() + "发起 " + courseDTO.getType().name() + "\n" +
                "> 训练时间为：" + DateUtil.parseDateToStr(courseDTO.getStartTime(), DateUtil.DATE_FORMAT_MMDDHHMI) +
                "~" + DateUtil.parseDateToStr(courseDTO.getEndTime(), "HH:mm") + "\n\n" +
                "> 报名方式：\n\n" +
                ">  1.通过手机钉钉小程序\"弓箭派\"报名”\n\n" +
                ">  2.通过艾特我查看课程表 快速报名”\n\n" +
                "> ![screenshot](http://47.111.20.91:8080/invite.jpg)\n");
    }

    @Override
    public void sendRankingNotify(RankingDTO dto, Long ranking, Long sort) {
        this.sendMsg( "琅琊榜TOP更新", "#### " + dto.getStudentName() + " 发起 琅琊榜-" + dto.getType().name() + " 打榜成功\n" +
                "> 打榜分数为 ：" + sort + " \n\n" +
                "> 当前排名为 ：" + ranking + "\n");
    }

    @Override
    public void sendRankingClean(Map<RankingTypeEnum, List<RankingDTO>> map) {

        StringBuffer msg = new StringBuffer();
        for (RankingTypeEnum type : map.keySet()) {
            msg.append("#### --【").append(type).append("榜】--\n");
            List<RankingDTO> rankingDTOS = map.get(type);
            for(int index = 0; index < rankingDTOS.size(); index++){
                RankingDTO rankingDTO = rankingDTOS.get(index);
                msg.append("第").append(index+1).append("名\"")
                        .append(rankingDTO.getStudentName()).append("\"获得")
                        .append((10 - index) * 10 ).append("积分\n\n");
            }
        }
        msg.append("--------\n\n");
        msg.append("为了防止积分膨胀，每个榜上射手只会获取最高名次的积分，新的赛季已经开始 欢迎大家积极打榜！！！ （～ 。～）");
        this.sendMsg( "琅琊榜赛季结束", msg.toString());
    }

    private void sendMsg(String title, String msg){
        for (String robotToken : tokens) {
            DingTalkClient client = new DefaultDingTalkClient(robotToken);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("markdown");
            OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
            markdown.setTitle(title);
            markdown.setText(msg);
            request.setMarkdown(markdown);
            try {
                OapiRobotSendResponse response = client.execute(request);
            } catch (ApiException e) {
                e.printStackTrace();
            }
        }
    }

}
